//
//  RankVC.m
//  Boss
//
//  Created by 孙昕 on 15/11/24.
//  Copyright © 2015年 孙昕. All rights reserved.
//

#import "RankVC.h"
#import "Header.h"
#import "WXApi.h"
#import "MyRankReq.h"
#import "SGActionView.h"
@interface RankVC ()

@end

@implementation RankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bHud=NO;
    self.title=@"排行";
    [self hideBackButton];
    [_tableMain setDelegateAndDataSource:self];
    [_tableMain setPageParam:@"page" Page:0];
    [_tableMain registarCell:@"RankCell" StrItem:@"RankItem"];
    [_tableMain reloadRequest:[NSString stringWithFormat:@"%@%@",serverUrl,@"/rank/top"] Param:@{
                                                                                                   @"username":USERNAME,
                                                                                                   @"pwd":PWD
                                                                                                   }];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"我的排名" style:UIBarButtonItemStylePlain target:self action:@selector(share)];
}

-(void)share
{
    [MyRankReq do:^(id req) {
        
    } Res:^(id res) {
        MyRankRes *obj=res;
        if(obj.code==0)
        {
            [TipView showWithTitle:@"我的排名" Tip:[NSString stringWithFormat:@"你的总排名为第%ld名，是否分享",[obj.data integerValue]] YesBlock:^{
                NSString *str=[NSString stringWithFormat:@"我的全国总排名为第%ld名,总积分%ld,快来和我pk吧！",[obj.data integerValue],[[UserDefaults sharedInstance] score]];
                [SGActionView showGridMenuWithTitle:@"选择分享平台"
                                         itemTitles:@[ @"微信好友", @"朋友圈" ]
                                             images:@[ [UIImage imageNamed:@"weixin"],
                                                       [UIImage imageNamed:@"pengyou"]]
                                     selectedHandle:^(NSInteger index) {
                                         if (index == 1) {
                                             [self sendLinkContent:str andScene:WXSceneSession];
                                         }else if(index == 2){
                                             [self sendLinkContent:str andScene:WXSceneTimeline];
                                         }
                                     }];
            } NoBlock:nil];
        }
        else
        {
            [TipView showWithTitle:@"错误" Tip:obj.msg Block:nil];
        }
    } ShowHud:YES];
    
}

- (void) sendLinkContent:(NSString *)content andScene:(int)scene
{
    WXMediaMessage *message = [WXMediaMessage message];
    if(scene==WXSceneTimeline)
    {
        message.title = content;
    }
    else
    {
        message.title = @"码农幻想";
        message.description = content;
    }
    [message setThumbImage:[UIImage imageNamed:@"icon120"]];
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = appUrl;
    message.mediaObject = ext;
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    
    if (![WXApi sendReq:req]) {
        [SVProgressHUD showErrorWithStatus:@"分享失败，请检查您的手机是否安装了微信！"];
    }
}

-(NSArray*)LazyTableViewDidFinishRequest:(LazyTableView *)tableview Request:(NSDictionary *)dic
{
    return dic[@"data"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
